<?php

class ProductsController extends Controller
{
	public $layout = 'default';
	public function filters()
	{
		return array(
			'accessControl',
		);
	}
	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',
				'actions'=>array('index', 'error', 'create', 'view'),
				'users'=>array('*'),
			),
			array('allow',
				'actions'=>array(''),
				'users'=>array('?'),
			),
			array('allow',
				'actions'=>array(''),
				'users'=>array('@'),
			),
			array('deny',
				'users'=>array('*'),
			),
		);
	}
	public function actionIndex()
	{
		$criteria=new CDbCriteria;
		$criteria->limit = 9;
		$criteria->order = "posteddate DESC";
		$model = Products::model()->findAll($criteria);
		$this->render('index', array("model"=>$model));
	}
	public function actionCreate(){
		if (isset($_POST['title']) && $_POST['title'] != '') {
			$product = new Products;

			$product->code = $_POST['code'];
			$product->name = $_POST['title'];
			$product->posteddate = date('Y:m:d H:i:s');
			$product->price = $_POST['price'];
			$product->unit = $_POST['unit'];
			$product->quantity = $_POST['quantity'];
			$product->description = $_POST['description'];
			$product->save(false);

			$category= $_POST['category'];
			// if (isset($_POST['description']) && $) {
			// 	# code...
			// }
			if (isset($_POST['nhasanxuat']) && $_POST['nhasanxuat'] !='') {
				$pro_attr = new ProductAttribute;
				$pro_attr->product_id = $product->id;
				$pro_attr->attribute_id = 1;
				$pro_attr->value = $_POST['nhasanxuat'];
				$pro_attr->save(false);
			}
			if (isset($_POST['duongkinh']) && $_POST['duongkinh'] !='') {
				$pro_attr = new ProductAttribute;
				$pro_attr->product_id = $product->id;
				$pro_attr->attribute_id = 2;
				$pro_attr->value = $_POST['duongkinh'];
				$pro_attr->save(false);
			}
			if (isset($_POST['vatlieu']) && $_POST['vatlieu'] !='') {
				$pro_attr = new ProductAttribute;
				$pro_attr->product_id = $product->id;
				$pro_attr->attribute_id = 3;
				$pro_attr->value = $_POST['vatlieu'];
				$pro_attr->save(false);
			}
			if (isset($_POST['loai']) && $_POST['loai'] !='') {
				$pro_attr = new ProductAttribute;
				$pro_attr->product_id = $product->id;
				$pro_attr->attribute_id = 4;
				$pro_attr->value = $_POST['loai'];
				$pro_attr->save(false);
			}
			if (isset($_POST['mausac']) && $_POST['mausac'] !='') {
				$pro_attr = new ProductAttribute;
				$pro_attr->product_id = $product->id;
				$pro_attr->attribute_id = 7;
				$pro_attr->value = $_POST['mausac'];
				$pro_attr->save(false);
			}
			if (isset($_POST['mat']) && $_POST['mat'] !='') {
				$pro_attr = new ProductAttribute;
				$pro_attr->product_id = $product->id;
				$pro_attr->attribute_id = 8;
				$pro_attr->value = $_POST['mat'];
				$pro_attr->save(false);
			}
			if (isset($_POST['khorong']) && $_POST['khorong'] !='') {
				$pro_attr = new ProductAttribute;
				$pro_attr->product_id = $product->id;
				$pro_attr->attribute_id = 9;
				$pro_attr->value = $_POST['khorong'];
				$pro_attr->save(false);
			}
			if (isset($_POST['chieudai']) && $_POST['chieudai'] !='') {
				$pro_attr = new ProductAttribute;
				$pro_attr->product_id = $product->id;
				$pro_attr->attribute_id = 10;
				$pro_attr->value = $_POST['chieudai'];
				$pro_attr->save(false);
			}
			if (isset($_POST['mat1']) && $_POST['mat1'] !='') {
				$pro_attr = new ProductAttribute;
				$pro_attr->product_id = $product->id;
				$pro_attr->attribute_id = 13;
				$pro_attr->value = $_POST['mat1'];
				$pro_attr->save(false);
			}
			if (isset($_POST['mat2']) && $_POST['mat2'] !='') {
				$pro_attr = new ProductAttribute;
				$pro_attr->product_id = $product->id;
				$pro_attr->attribute_id = 14;
				$pro_attr->value = $_POST['mat2'];
				$pro_attr->save(false);
			}
			if (isset($_POST['beday']) && $_POST['beday'] !='') {
				$pro_attr = new ProductAttribute;
				$pro_attr->product_id = $product->id;
				$pro_attr->attribute_id = 15;
				$pro_attr->value = $_POST['beday'];
				$pro_attr->save(false);
			}
			if (isset($_POST['duongkinhlo']) && $_POST['duongkinhlo'] !='') {
				$pro_attr = new ProductAttribute;
				$pro_attr->product_id = $product->id;
				$pro_attr->attribute_id = 16;
				$pro_attr->value = $_POST['duongkinhlo'];
				$pro_attr->save(false);
			}
			if (isset($_POST['khoangcachlo']) && $_POST['khoangcachlo'] !='') {
				$pro_attr = new ProductAttribute;
				$pro_attr->product_id = $product->id;
				$pro_attr->attribute_id = 17;
				$pro_attr->value = $_POST['khoangcachlo'];
				$pro_attr->save(false);
			}
			if (isset($_POST['duongkinhday1']) && $_POST['duongkinhday1'] !='') {
				$pro_attr = new ProductAttribute;
				$pro_attr->product_id = $product->id;
				$pro_attr->attribute_id = 18;
				$pro_attr->value = $_POST['duongkinhday1'];
				$pro_attr->save(false);
			}
			if (isset($_POST['duongkinhday2']) && $_POST['duongkinhday2'] !='') {
				$pro_attr = new ProductAttribute;
				$pro_attr->product_id = $product->id;
				$pro_attr->attribute_id = 19;
				$pro_attr->value = $_POST['duongkinhday2'];
				$pro_attr->save(false);
			}
			if (isset($_POST['buoc']) && $_POST['buoc'] !='') {
				$pro_attr = new ProductAttribute;
				$pro_attr->product_id = $product->id;
				$pro_attr->attribute_id = 20;
				$pro_attr->value = $_POST['buoc'];
				$pro_attr->save(false);
			}
			if (isset($_POST['khoang']) && $_POST['khoang'] !='') {
				$pro_attr = new ProductAttribute;
				$pro_attr->product_id = $product->id;
				$pro_attr->attribute_id = 21;
				$pro_attr->value = $_POST['khoang'];
				$pro_attr->save(false);
			}
			$url= Yii::app()->createUrl('/Products/products/view')."&id=$product->id";
			echo "<script>window.location.href = '".$url."';</script>";
			// $this->redirect($url);
			// $this->redirect(array('/Products/products/view','id'=>$product->id));
		}
	}
	public function actionView($id){
		//http://localhost/ildongvina/index.php?r=Products/products/view&id=13
		//$layout = "view";
		$product = Products::model()->findByPk($id);
		$pro_attr = ProductAttribute::model()->findAll("product_id=$id");
		if (isset($product)) {
			$this->render('view', array('product'=>$product, 'pro_attr'=>$pro_attr));	
		} else {
			$this->redirect(array('/Home/default/error'));
		}
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}